package com.lan.controller.sys;

import com.lan.controller.BaseController;
import com.lan.service.sys.CacheService;
import com.lan.utils.cache.RedisUtil;
import com.lan.viewObject.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * <p>
 *  缓存 Controller
 * </p>
 *
 * @author shadow
 * @since 2022-08-16
 */
@Controller
@RequestMapping("/cache")
public class CacheController extends BaseController {

    @Resource
    private CacheService cacheService;
    @Resource
    private RedisUtil cacheUtil;

    /**
     * 健康检查 -> 查看缓存是否可用
     *
     * */
    @ResponseBody
    @GetMapping("/healthCheck")
    public Result healthCheck() {
        LOGGER.info("<<<<<<<<<< healthCheck >>>>>>>>>>");
        int set = cacheUtil.hSet("healthKey", "healthField", "healthValue", 60*5L);
        if (set == 0) {
            return Result.error("healthCheck failed >>>>> hSet");
        }
        String get = cacheUtil.hGet("healthKey", "healthField");
        if (get == null || get.isEmpty()) {
            return Result.error("healthCheck failed >>>>> hGet");
        }
        return Result.ok();
    }

}
